import 'package:WebTest/Route/about.dart';
import 'package:WebTest/Route/cupertinoPageRoute.dart';
import 'package:WebTest/Route/detail.dart';
import 'package:WebTest/Route/routerManager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

// MaterialPageRoute -> PageRoute -> ModalRoute -> TransitionRoute -> OverlayRoute -> Route
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      theme: ThemeData(
        platform: TargetPlatform.iOS,
      ),
      routes: RouterManager.routers,
      initialRoute: RouterManager.initialRoute,
      onGenerateRoute: RouterManager.generateRouter,
      onUnknownRoute: RouterManager.unknownRouter,
    );
  }
}

class HomePage extends StatefulWidget {
  static const String routerName = "/";

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _message = "default message";

  @override
  Widget build(BuildContext context) {
//    Navigator.of(context).pushNamed();
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_message ?? "message"),
            RaisedButton(
              child: Text("跳转到详情"),
              onPressed: () => _jumpDetail(context),
            ),
            RaisedButton(
              child: Text("跳转到关于"),
              onPressed: () => _jumpAbout(context),
            ),
            RaisedButton(
              child: Text("跳转到详情2"),
              onPressed: () => _jumpDetail2(context),
            ),
            RaisedButton(
              child: Text("跳转到设置"),
              onPressed: () => _jumpSettings(context),
            ),
            RaisedButton(
              child: Text("跳转右划"),
              onPressed: () => _jumpRightBack(context),
            ),
          ],
        ),
      ),
    );
  }

  void _jumpDetail(BuildContext context) {
    Future result =
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return HomeDetailPage("a home message");
    }));
    result.then((res) {
      setState(() {
        _message = res;
      });
    });
  }
  void _jumpDetail2(BuildContext context) {
    Future result =
        Navigator.of(context).pushNamed(HomeDetailPage.routerName,arguments: "a home2 message");
    result.then((res) {
      setState(() {
        _message = res;
      });
    });
  }

  void _jumpAbout(BuildContext context) {
    Future result = Navigator.of(context)
        .pushNamed(HomeAbout.routerName, arguments: "a home message");
    result.then((res) {
      setState(() {
        _message = res;
      });
    });
  }
  void _jumpSettings(BuildContext context) {
    Future result = Navigator.of(context)
        .pushNamed("/settings", arguments: "a home message");
    result.then((res) {
      setState(() {
        _message = res;
      });
    });
  }
  void _jumpRightBack(BuildContext context) {
    Future result =Navigator.of(context).push(
        CupertinoPageRoute(builder: (BuildContext context){
          // 每次点击再打开一次当前的页面
          return RightBackDemo();
        }));
    result.then((res) {
      setState(() {
        _message = res;
      });
    });
  }
}
